package lucene.server.web.controller.admin;


import lombok.extern.slf4j.Slf4j;
import lucene.server.common.model.Result;
import lucene.server.common.utils.StringUtil;
import lucene.server.web.dao.SystemDao;
import lucene.server.web.task.TaskPool;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.UUID;


@RestController
@RequestMapping("/admin/canal")
@Slf4j
public class CanalController {
/*
    @GetMapping(value = "/get")
    public Result<?> getCanal(HttpServletRequest req) throws SQLException {
        return  Result.OK( SystemDao.getAll("cdc_canal", CanalConfig.class));
    }
    @GetMapping(value = "/getStatus")
    public Result<?> getStatus(HttpServletRequest req)  {
        return  Result.OK( );
    }
    @PostMapping(value = "/runTask")
    public Result<?> runTask(@RequestBody CanalConfig canal)  {
        return  Result.OK( );
    }
    @PostMapping(value = "/update")
    public Result<?> updateConn(@RequestBody CanalConfig canal, HttpServletRequest req) {

        //如果id没有，先判断是否存在
        if (StringUtil.isEmpty(canal.getId()) && SystemDao.checkExists(canal, "name")) {
            return Result.error("Canal名称已存在");
        }
        if (StringUtil.isEmpty(canal.getId())){
            canal.setId( UUID.randomUUID().toString().replace("-", ""));
        }else{
            CanalConfig oldCanal = SystemDao.findOne(canal, CanalConfig.class);
            TaskPool.shutdown("canal_"+oldCanal.getName()+"重建任务");
        }
        String error = checkCanal(canal);
        if (!StringUtil.isEmpty(error)){
            return Result.error(error);
        }

        SystemDao.insertOrUpdate(canal);
        CanalTask canalTask = new CanalTask(canal);
        TaskPool.addTask(canalTask);
        return Result.OK();
    }

    @PostMapping(value = "/delete")
    public Result<?> deleteConn(@RequestBody CanalConfig canal, HttpServletRequest req) {
        SystemDao.delete(canal);
        TaskPool.shutdown("canal_"+canal.getName());
        return Result.OK();
    }
    private String checkCanal(CanalConfig canal){
        String error = "";
        error += StringUtil.assertNull(canal.getName(),"名称不能为空\n");
        error += StringUtil.assertNull(canal.getHost(),"Host不能为空\n");
        error += StringUtil.assertNull(canal.getPort() ,"端口不能为空\n");
        error += StringUtil.assertNull(canal.getDestination(),"Destination不能为空\n");
        return error;
    }
*/
}

